#include <stdio.h>
#include <iostream>
#include "XmlHeaders.h"
#include "DtdHeaders.h"
#include "HTMLGenerator.h"


using namespace std;

extern FILE* xmlin;
extern FILE* xslin;
extern FILE* dtdin;
extern int xmlparse(XML **x);
extern int xslparse(XML **x);
extern int dtdparse(DTD **d);
extern bool check_conformity(const XML *xml,const DTD *dtd);
extern bool matchRegExp(string, string);



/* You should specify the xml file path while executing inline command */
int main(int argc, char **argv)  
{
  int err;
  string answer;
  DTD* dtd = NULL;
  XML* xml = NULL;
  XML * xsl = NULL;
  FILE * fid = NULL;

  if (argc != 2 )
  {
    printf("Error :Should be used like this : \n $ ./analyse doc.xml [output.html]  (where doc.xml is an xml document, and output.html the html file you want to generate) \n");
    return 0;
  }
  
  //Getting XML file
  fid = fopen(argv[1], "r");
  xmlin = fid;
  if( xmlin == NULL)
  {
    cout << "File : \"" << argv[1] << "\" not found." << endl;
    return 0;
  }
  
  printf("1) Checking syntaxically the XML file..... \n");
 //Checking syntaxically the xml file
  err = xmlparse(&xml);

  if (err != 0 ) //|| ! xml->isValid() )
  {
	  printf("\tError : XML File \"%s\" has %d  syntaxical error(s).\n",argv[1], err);
	  return 0;
  }
  else
  {
	if(xml->getIsValid() == false)
		printf("\tError : XML File: \"%s\" is not syntaxically correct, but could be corrected using TIPS (we have generated a correct version).\n", argv[1]);
	else		
		printf("\tXML File: \"%s\"  is syntaxically correct.\n", argv[1]);
	  printf("\tDo you want to display the structure of the XML document? (y-n) : ");
	  cin>>answer;
          if(answer=="y" || answer=="Y" || answer=="yes" || answer=="Yes" || answer=="YES")
	{
		printf("---- Structure of XML ------\n");
		cout<<xml->toString();
	}
  }
  fclose(fid);
  
 
  printf("\n\n2) Checking syntaxically the DTD file associated.....\n");

  if ( xml->getDTD().empty() )
  {
   cout << "\tError : no dtd is referred in the xml file" << endl;
   return 0;
  }
  
  
 //Getting the dtd file
  string dtd_name = xml->getDTD();
  dtdin = fopen(dtd_name.c_str(), "r");
  
  if( dtdin == NULL)
  {
    cout << "\tError : DTD file  : \"" << dtd_name << "\" not found." << endl;
    return 0;
  }
  
  //Checking the syntax of the DTD
  err = dtdparse(&dtd); 
  if (err != 0)
  {
	  printf("\tError : DTD file  : \"%s\" has %d syntaxical error(s).\n",dtd_name.c_str(), err);
	  return 0;
  }
  else
  {
	  printf("\tDTD file : \"%s\"  is syntaxically correct.\n", dtd_name.c_str());
	  printf("\n\n3) Checking validity of the DTD...\n");
	  if (dtd->makeDTD() == -1  || !dtd->isValid() )
	  {
			printf("\tError: The DTD is not valid\n");
			return 0;
	  }
	  else 
	  {
		printf("\tThe DTD is valid! Do you want to display its structure?(y-n) :");
	  	cin>>answer;
	  	if(answer=="y" || answer=="Y" || answer=="yes" || answer=="Yes" || answer=="YES")
	  	{
		    dtd->display();
	  	}
	  }

  }

  printf("\n\n4) Checking the conformity of XML to its  DTD.\n");
  // Checking conformity of xml file to its associated DTD
  if(check_conformity(xml,dtd)) cout << "\tXML document is conform to its DTD"<< endl;
  else 
  {
	cout << "\tError : XML document is not conform to its dtd \n "<< endl;
	printf("\tTo see errors open the file log_errors.txt, do you want to see the log right now ? (y-n) : ");
	cin>>answer;
	if(answer=="y" || answer=="Y" || answer=="yes" || answer=="Yes" || answer=="YES")
	{
		system("gedit log_errors.txt& ");
	}
	return 0;
  }
  fclose(dtdin);
//-------------------------------------------------------------------------------------------------------------------

  printf("\n\n5) Checking syntaxically the XSL file associated.....\n");
 //Getting the xsl file
  string xsl_name = xml->getStyleSheet();
  fid = fopen(xsl_name.c_str(), "r");
  xslin = fid;
  
  if( xslin == NULL)
  {
    cout << "\tError : XSL file  : \"" << xsl_name << "\" not found." << endl;
    return 0;
  }
  
  //Checking the syntax of the xsl
  err = xslparse(&xsl);
  if (err != 0)
  {
	  printf("\tError : XSL file  : \"%s\" has %d syntaxical error(s).\n",xsl_name.c_str(), err);
	  fclose(fid);
	  return 0;
  }
  else
  {
	  printf("\tXSL file : \"%s\"  is syntaxically correct.\n", xsl_name.c_str());
	  printf("\tDo you want to display the structure of the XSL document? (y-n) : ");
	  cin>>answer;
          if(answer=="y" || answer=="Y" || answer=="yes" || answer=="Yes" || answer=="YES")
	  {
		printf("##### Structure of XSL##### \n");
		cout<<xsl->toString();
	  } 

  }
  fclose(fid);
  if (argc < 3 )
  {
      argv[2] = "output.html";
  }
  printf("\n\n6)Generating the HTML File..... \n");
  /*HTMLGenerator htmlGen(xsl);
  string html = htmlGen.treeToHTML(xml->getRoot());
  cout << "html html" << endl;
  cout << html << endl;*/
  //Generating HTML from XML and XSL
  //tree_transformation(xml, xsl, argv[2]);
  printf("\tDo you want to see the HTML generated? (y-n) : ");
  cin>>answer;
  if(answer=="y" || answer=="Y" || answer=="yes" || answer=="Yes" || answer=="YES")
  {
	string file = argv[2];
	string command = "firefox " + file + "&";
 	system(command.c_str());
  } 
  
  printf("\n\n Finished without error");

  return 0;
}
